package leetcode.leetcode_189;


import java.util.ArrayList;
import java.util.Arrays;

class Solution {
    public void rotate1(int[] nums, int k) {
        k = k % nums.length;
        reverse(nums,0,nums.length-1);
        reverse(nums,0,k-1);
        reverse(nums,k,nums.length-1);
    }
    public void reverse (int [] nums, int start, int end){
        while(start<end){
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
    public void rotate(int[] nums, int k) {
        ArrayList<Integer> list = new ArrayList<>();
        int n = nums.length;
        k = k % n;
        for (int i = n-1; i >=0; i--){
            if (k+i>=n){
                list.add(nums[i]);
            }
            else {
                nums[ i+k] = nums[i];
            }
        }
        for (int i = 0; i < k; i++){
            nums[i] = list.get(k-i-1);
        }
    }
}